<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="活动ID:">
        <el-input v-model="queryParams.activityId" size="small" placeholder="请输入活动ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="SPU ID:">
        <el-input v-model="queryParams.spuId" size="small" placeholder="请输入SPU ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="SKU ID:">
        <el-input v-model="queryParams.skuId" size="small" placeholder="请输入SKU ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称:">
        <el-input v-model="queryParams.goodsName" size="small" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订阅人ID:">
        <el-input v-model="queryParams.subscriberId" size="small" placeholder="请输入订阅人ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订阅人:">
        <el-input v-model="queryParams.subscriber" size="small" placeholder="请输入订阅人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订阅人电话:">
        <el-input v-model="queryParams.subscriberPhone" size="small" placeholder="请输入订阅人电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订阅时间:">
        <el-date-picker v-model="queryParams.subscribeTime" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择订阅时间" clearable />
      </el-form-item>
      <el-form-item label="通知发送时间:">
        <el-date-picker v-model="queryParams.sendTime" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择通知发送时间" clearable />
      </el-form-item>
      <el-form-item label="所属店铺ID:">
        <el-input v-model="queryParams.storeId" size="small" placeholder="请输入所属店铺ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:flashSaleNotice:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:flashSaleNotice:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:flashSaleNotice:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="活动ID" align="center" prop="activityId" />
      <el-table-column label="SPU ID" align="center" prop="spuId" />
      <el-table-column label="SKU ID" align="center" prop="skuId" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="订阅人ID" align="center" prop="subscriberId" />
      <el-table-column label="订阅人" align="center" prop="subscriber" />
      <el-table-column label="订阅人电话" align="center" prop="subscriberPhone" />
      <el-table-column label="订阅时间" align="center" prop="subscribeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subscribeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通知内容" align="center" prop="noticeContent" />
      <el-table-column label="通知发送时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属店铺ID" align="center" prop="storeId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['marketing:flashSaleNotice:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['marketing:flashSaleNotice:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="flashSaleNoticeFormDialog.title" :visible.sync="flashSaleNoticeFormDialog.visible" :close-on-click-modal="false" :before-close="resetFlashSaleNoticeFormDialog" width="500px" append-to-body>
      <el-form ref="flashSaleNoticeForm" :model="flashSaleNoticeFormData" :rules="flashSaleNoticeFormRules" label-width="80px">
        <el-form-item label="活动ID" prop="activityId">
          <el-input v-model="flashSaleNoticeFormData.activityId" placeholder="请输入活动ID" />
        </el-form-item>
        <el-form-item label="SPU ID" prop="spuId">
          <el-input v-model="flashSaleNoticeFormData.spuId" placeholder="请输入SPU ID" />
        </el-form-item>
        <el-form-item label="SKU ID" prop="skuId">
          <el-input v-model="flashSaleNoticeFormData.skuId" placeholder="请输入SKU ID" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="flashSaleNoticeFormData.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="订阅人ID" prop="subscriberId">
          <el-input v-model="flashSaleNoticeFormData.subscriberId" placeholder="请输入订阅人ID" />
        </el-form-item>
        <el-form-item label="订阅人" prop="subscriber">
          <el-input v-model="flashSaleNoticeFormData.subscriber" placeholder="请输入订阅人" />
        </el-form-item>
        <el-form-item label="订阅人电话" prop="subscriberPhone">
          <el-input v-model="flashSaleNoticeFormData.subscriberPhone" placeholder="请输入订阅人电话" />
        </el-form-item>
        <el-form-item label="订阅时间" prop="subscribeTime">
          <el-date-picker v-model="flashSaleNoticeFormData.subscribeTime" type="date" value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择订阅时间" clearable />
        </el-form-item>
        <el-form-item label="通知内容">
          <tinymce v-model="flashSaleNoticeFormData.noticeContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="通知发送时间" prop="sendTime">
          <el-date-picker v-model="flashSaleNoticeFormData.sendTime" type="date" value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择通知发送时间" clearable />
        </el-form-item>
        <el-form-item label="所属店铺ID" prop="storeId">
          <el-input v-model="flashSaleNoticeFormData.storeId" placeholder="请输入所属店铺ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="flashSaleNoticeFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmFlashSaleNoticeFormDialog">确 定</el-button>
        <el-button @click="resetFlashSaleNoticeFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FlashSaleNoticeApi from '@/api/marketing/FlashSaleNoticeApi'
import Tinymce from '@/components/Tinymce'

const defaultFlashSaleNoticeFormData = {
  activityId: null,
  spuId: null,
  skuId: null,
  goodsName: null,
  subscriberId: null,
  subscriber: null,
  subscriberPhone: null,
  subscribeTime: null,
  noticeContent: null,
  sendTime: null,
  storeId: null
}
export default {
  name: 'FlashSaleNoticeList',
  components: { Tinymce },
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        activityId: null,
        spuId: null,
        skuId: null,
        goodsName: null,
        subscriberId: null,
        subscriber: null,
        subscriberPhone: null,
        subscribeTime: null,
        noticeContent: null,
        sendTime: null,
        storeId: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      flashSaleNoticeFormDialog: {
        title: '',
        visible: false
      },
      flashSaleNoticeFormData: Object.assign({}, defaultFlashSaleNoticeFormData),
      flashSaleNoticeFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openFlashSaleNoticeFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FlashSaleNoticeApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openFlashSaleNoticeFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      FlashSaleNoticeApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FlashSaleNoticeApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openFlashSaleNoticeFormDialog(id) {
      this.flashSaleNoticeFormDialog.visible = true
      if (id) {
        this.flashSaleNoticeFormDialog.title = '限时折扣活动通知-修改'
        FlashSaleNoticeApi.get(id).then(response => {
          this.flashSaleNoticeFormData = {
            ...this.flashSaleNoticeFormData,
            ...response.data
          }
        })
      } else {
        this.flashSaleNoticeFormDialog.title = '限时折扣活动通知-添加'
      }
    },
    confirmFlashSaleNoticeFormDialog() {
      this.$refs['flashSaleNoticeForm'].validate(valid => {
        if (valid) {
          if (this.flashSaleNoticeFormData.id) {
            FlashSaleNoticeApi.edit(this.flashSaleNoticeFormData).then(response => {
              this.$message.success('修改成功')
              this.resetFlashSaleNoticeFormDialog()
              this.handleQuery()
            })
          } else {
            FlashSaleNoticeApi.add(this.flashSaleNoticeFormData).then(response => {
              this.$message.success('添加成功')
              this.resetFlashSaleNoticeFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetFlashSaleNoticeFormDialog() {
      this.flashSaleNoticeFormDialog = {
        title: '',
        visible: false
      }
      this.flashSaleNoticeFormData = Object.assign({}, defaultFlashSaleNoticeFormData)
      this.resetForm('flashSaleNoticeForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
